define([
    'jquery',
    'underscore',
    'backbone',
    //'text!templates/form/form_add_admin.html'
    ], function($, _, Backbone,loginTemplate){
        var $html_loadding = '<div class="caculation-report"><img style="margin-left:170px" src="'+$template_admincp_url+'img/standard/loaders/loading64.gif"/></div>';
        var caseProView = Backbone.View.extend({
            el: $("#main"),
            initialize: function(){
				
            },
            events: {
                
            },
            
            index: function(){
                var url = location.href;
                if(typeof oTable != "undefined"){
                    $("#table_case_product").dataTable().fnDestroy();
                }
                oTable = $('#table_case_product').dataTable({
                    "sDom": "<'row'<'col-lg-8'l><'col-lg-1 button_create'><'col-lg-3'f>r>t<'row'<'col-lg-6'i><'col-lg-6'p>>",
                    "sPaginationType": "bootstrap",
                    "bJQueryUI": false,
                    "bAutoWidth": false,
                    "oLanguage": {
                        "sUrl": $base_url+"public/templates/admincp/js/language/datatable.vn.txt"
                    },
                    "sScrollX": "100%",
                    //"sScrollXInner": "100%",
                    "bScrollCollapse": true,
                    "iDisplayLength": 10,
                    "iDisplayStart": 0,
                    "bProcessing": true,
                    "bRetrieve": true,
                    "aaSorting": [],
                    "bServerSide": true,
                    "sAjaxSource": url,
                    "fnServerData": function(sSource, aoData, fnCallback, oSettings) {
                        if(typeof filters != "undefined"){
                            obj_tmp = new Object;
                            $.each(filters, function(name, value){
                                obj_tmp[name] = value;
                            });
                            aoData.push({
                                name: "myFilter",
                                value: JSON.stringify(obj_tmp)
                            });
                        }
                        $.ajax( {
                            "dataType": 'json',
                            "type": "POST",
                            "url": sSource,
                            "data": aoData,
                            success: function(rs){
                                ids = rs.my_id;
                                fnCallback(rs);
                            }
                        });

                    },
                    "aoColumns": [
                        {
                            // "mData": "admin_name",
                            "sType": "string",
                            "swidth": "25%",
                            "sClass": "string",
                            "sName": "cata_name"
                        },
                        {
                            // "mData": "admin_name",
                            "sType": "string",
                            "swidth": "25%",
                            "sClass": "string",
                            "sName": "pro_title"
                        },
                        {
                            // "mData": "admin_name",
                            "sType": "string",
                            "swidth": "25%",
                            "sClass": "string",
                            "sName": "case_title"
                        },
                        {
                            // "mData": "admin_name",
                            "sType": "date",
                            "swidth": "25%",
                            "sClass": "string",
                            "sName": "date_add"
                        },
                        {
                            // "mData": "action",
                            "sType": "string",
                            "swidth": "3%",
                            "sClass": "center",
                            "bSortable": false
                        }
                    ],

                    fnInitComplete: function()
                    {
                        $(this).slideDown(200);
                        $(".button_create").html('<button class="btn btn-primary btn-create" type="button">Create New</button>');
                        $(".btn-create").click(function(){
                            window.location.href = $admincp_url+'case_product/add';
                        });
                    }
                });
                $('.dataTables_length select').uniform();
                $('.dataTables_paginate > ul').addClass('pagination');
            },

            add: function(){
                // Form Validate
                $('#catalogue_product').select2({placeholder: "Select Catalogue"});
                $('#product').select2({placeholder: "Select Product"});
                $("#form").validate({
                    ignore: null,
                    ignore: 'input[type="hidden"]',
                    rules: {
                        catalogue_product: "required",
                        product: "required",
                        case_title: "required",
                        case_img: {
                            fileType: {
                                types: ["png", "jpeg", "gif"]
                            },
                            maxFileSize: {
                                "unit": "MB",
                                "size": 2   
                            },
                            // minFileSize: {
                            //     "unit": "MB",
                            //     "size": 1
                            // }
                        },
                        agree: "required"
                    },
                    submitHandler: function(form){
                        form.submit();
                    }
                });
                $("#catalogue_product").change(function(){
                    var cata_id = $(this).val();
                    $.ajax({
                        url: $base_url+'admincp/product/process',
                        type: 'post',
                        data: 'cata_id='+cata_id,
                        async: false,
                        success: function(rs){
                            rs = JSON.parse(rs);
                            $("#product").html(rs.html);
                        }
                    });
                });
            },

            edit: function(){
                // Form Validate
                $('#catalogue_product').select2({placeholder: "Select Catalogue"});
                // $('#product').select2({placeholder: "Select Product"});
                $("#form").validate({
                    ignore: null,
                    ignore: 'input[type="hidden"]',
                    rules: {
                        catalogue_product: "required",
                        product: "required",
                        case_title: "required",
                        case_img: {
                            fileType: {
                                types: ["png", "jpeg", "gif"]
                            },
                            maxFileSize: {
                                "unit": "MB",
                                "size": 2   
                            },
                            // minFileSize: {
                            //     "unit": "MB",
                            //     "size": 1
                            // }
                        },
                        agree: "required"
                    },
                    submitHandler: function(form){
                        form.submit();
                    }
                });
                var cata_id = $('#catalogue_product').val();
                var pro_id = $('#proHide').val();
                if(cata_id != 0){
                    $.ajax({
                        url: $base_url+'admincp/product/process',
                        type: 'post',
                        data: 'cata_id='+cata_id+'&pro_id='+pro_id,
                        async: false,
                        success: function(rs){
                            rs = JSON.parse(rs);
                            $("#product").html(rs.html);
                        }
                    });
                }else{
                    $("#product").find("option").remove();
                    $("#product").append("<option value='0'>Not Data</option>");
                }
                $("#catalogue_product").change(function(){
                    var cata_id = $(this).val();
                    $.ajax({
                        url: $base_url+'admincp/product/process',
                        type: 'post',
                        data: 'cata_id='+cata_id,
                        async: false,
                        success: function(rs){
                            rs = JSON.parse(rs);
                            $("#product").html(rs.html);
                        }
                    });
                });
            },
            
            render: function(){
                if(typeof this[$app.action] != 'undefined'){
                    new this[$app.action];
                }
            }
        });
        return caseProView = new caseProView;
    });
